<?php


##  TODO: make all time work, test results

 
session_start();

$user = $_SESSION['username'];
if (!$user) { header('location: login.php'); exit; }

$sort = $_GET['sort'];
$filter = $_POST['filter_field']; if (!$filter) { $filter = $_GET['filter_field'];}
$filter_data = $_POST['dataa']; if (!$filter_data) { $filter_data = $_GET['dataa'];}
include "../../includes/functions.php";
echo db_connect();

if ($_GET['msg'] == 'd1') $msg = "Playlist Deleted";
if ($_GET['msg'] == 'd2') $msg = "Error deleting playlist";
 
 /// put auth check here


$day = date("l"); //echo $day;

$start_date = $_POST['data1']; if ($start_date && strlen($start_date) < 12) $start_date= $start_date . " 00:00:00";
$end_date = $_POST['data2']; if ($end_date && strlen($end_date) < 12) $end_date= $end_date . " 23:59:59";   

if ($start_date) { 
		if ($end_date) {
			$cl = "WHERE time_played > '$start_date' AND time_played < '$end_date'";        
  		} else {
	  		$cl = "WHERE time_played > '$start_date'";
		}
	} else { # no start date
	 	if ($end_date) {
		 	$cl = "WHERE time_played < '$end_date'";
		}     
	}


$query="SELECT  album, artist, count(DISTINCT playlist_id) as times_played 
FROM playlist_items $cl
GROUP BY album, artist
ORDER BY times_played DESC
LIMIT 30 ";
$result= mysql_query($query);

//echo $query; exit;
 
 if ($start_date || $end_date) { 
	$period = dateFormat($start_date,'',2) . " - " . dateFormat($end_date,'',2); 
	} else {
	$period = "all time";	
	}

 ?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Playlists</title>
<link href="../../css/style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">

</script>
<script type="text/javascript" src="../../calendar_picker.js"></script>      
</head>

<body bgcolor="#eeeeee">
<a href='index.php'><< ADMINISTRATION Home </a><br><br>
<h1>REPORT: Top 30 most played albums, <?=$period?></h1>
<p><?=$msg?></p>

<?php


$today = substr(date('Y-m-d'),0,10); //echo "today: $today";


echo "<table><tr><th class='report_subhead' style='width:150px'>Album</th>
<th class='report_subhead' style='width:150px'>Artist</th>
<th class='report_subhead' style='width:150px'>Times Played</th>
<th class='report_subhead' style='width:150px'>View Album</th>
</tr>";  

while ($row= mysql_fetch_array($result)) {
		$album= ucWords($row['album']);
		$artist=ucWords($row['artist']); 
		$times_played = $row['times_played'];
		$ml_album_id= $row['ml_album_id']; 
		if (!$ml_album_id) {
			$ml_select = 'SELECT album_id FROM ml_albums WHERE artist = "'.$artist.'" AND title = "'.$album.'"';
			$ml_result = mysql_query ($ml_select); if (!$ml_result) die(mysql_error().$ml_select);
			if ($ml_row = mysql_fetch_array($ml_result)) {
			 $ml_album_id = $ml_row['album_id'];  
			} else {
			  $ml_album_id = ""; 
			}
			
		}
		
		if ($ml_album_id) { $album_link = "<a href='../../edit_album.php?mode=edit&aid=$ml_album_id' target='blank' style='color:blue;'>Album in Library/Playbox</a>";} else {$album_link="";}
		 echo "<tr'>
			<td class='report_item' style='width:150px'>$album</td>
			<td class='report_item' style='width:150px'>$artist</td>
			<td class='report_item' style='width:150px;text-align:center;'>$times_played</td>
			<td class='report_item' style='width:150px'>$album_link</td>
		</tr>";
		
}


?>  
</table>
</body>
</html>
